<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>离线海图下载工具</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <link rel="stylesheet" href="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.css" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css">
    <style>
        #map {
            height: 500px;
            width: 100%;
        }
        
        .progress-bar {
            height: 20px;
            background-color: #f0f0f0;
            border-radius: 5px;
            margin: 10px 0;
            overflow: hidden;
        }
        
        .progress-bar-inner {
            height: 100%;
            background-color: #4CAF50;
            width: 0%;
            transition: width 0.3s;
        }
        
        .task-item {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
            background-color: #f9f9f9;
            border-left: 4px solid #999;
        }
        
        .task-item.completed {
            border-left-color: #4CAF50;
        }
        
        .task-item.failed {
            border-left-color: #F44336;
        }
        
        .task-item.downloading {
            border-left-color: #2196F3;
        }
    </style>
</head>

<body class="bg-gray-100">
    <div class="container mx-auto px-4 py-6">
        <header class="mb-6">
            <h1 class="text-3xl font-bold text-center text-gray-800">离线海图下载工具</h1>
            <p class="text-center text-gray-600 mt-2">专为海工船设计，支持在无网络环境下使用</p>
        </header>

        <div class="bg-white p-6 rounded-lg shadow-md mb-6">
            <h2 class="text-xl font-semibold mb-4">区域选择</h2>
            <div id="map" class="rounded-lg shadow-sm"></div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-4">
                <div>
                    <h3 class="text-lg font-medium mb-2">经纬度范围</h3>
                    <div class="grid grid-cols-2 gap-2">
                        <div>
                            <label class="block text-sm font-medium text-gray-700">最小纬度</label>
                            <input type="number" id="minLat" step="0.01" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">最小经度</label>
                            <input type="number" id="minLon" step="0.01" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">最大纬度</label>
                            <input type="number" id="maxLat" step="0.01" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">最大经度</label>
                            <input type="number" id="maxLon" step="0.01" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                    </div>
                </div>

                <div>
                    <h3 class="text-lg font-medium mb-2">下载设置</h3>
                    <div class="space-y-2">
                        <div>
                            <label class="block text-sm font-medium text-gray-700">地图名称</label>
                            <input type="text" id="mapName" placeholder="例如：浙江舟山" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700">瓦片源</label>
                            <select id="tileSource" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                <option value="osm" selected>OpenStreetMap (标准)</option>
                                <option value="otm">OpenTopoMap (地形)</option>
                                <option value="esri">ESRI WorldImagery (卫星)</option>
                                <option value="esri_ocean">ESRI Ocean (海域专用)</option>
                                <option value="google">Google Maps (标准)</option>
                                <option value="google_satellite">Google Satellite (卫星)</option>
                                <option value="google_terrain">Google Terrain (地形)</option>
                                <option value="google_hybrid">Google Hybrid (混合)</option>
                                <option value="carto">CartoDB Light (轻量)</option>
                                <option value="custom">自定义</option>
                            </select>
                            <p class="text-xs text-gray-500 mt-1">*海域地图推荐使用ESRI Ocean或Google Satellite</p>
                        </div>
                        <div id="customTileUrlContainer" class="hidden">
                            <label class="block text-sm font-medium text-gray-700">自定义瓦片URL</label>
                            <input type="text" id="customTileUrl" placeholder="https://example.com/{z}/{x}/{y}.png" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                        </div>
                        <div class="grid grid-cols-2 gap-2">
                            <div>
                                <label class="block text-sm font-medium text-gray-700">最小缩放级别</label>
                                <select id="minZoom" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                    <option value="4">4</option>
                                    <option value="5" selected>5</option>
                                    <option value="6">6</option>
                                    <option value="7">7</option>
                                    <option value="8">8</option>
                                </select>
                            </div>
                            <div>
                                <label class="block text-sm font-medium text-gray-700">最大缩放级别</label>
                                <select id="maxZoom" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
                                    <option value="8">8</option>
                                    <option value="9">9</option>
                                    <option value="10">10</option>
                                    <option value="11">11</option>
                                    <option value="12" selected>12</option>
                                    <option value="13">13</option>
                                    <option value="14">14</option>
                                    <option value="15">15</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="mt-6 text-right">
                <button id="calculateSize" class="mr-2 px-4 py-2 bg-gray-500 text-white rounded-md hover:bg-gray-600 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-opacity-50">计算大小</button>
                <button id="downloadBtn" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50">开始下载</button>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div class="bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-xl font-semibold mb-4">下载任务</h2>
                <div id="tasksList" class="space-y-4">
                    <p class="text-gray-500 text-center">暂无下载任务</p>
                </div>
            </div>

            <div class="bg-white p-6 rounded-lg shadow-md">
                <h2 class="text-xl font-semibold mb-4">离线地图文件</h2>
                <div id="mbtilesList" class="space-y-4">
                    <p class="text-gray-500 text-center">暂无地图文件</p>
                </div>
            </div>
        </div>

        <!-- 离线查看地图模态框 -->
        <div id="viewMapModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden">
            <div class="bg-white p-6 rounded-lg shadow-lg w-full max-w-5xl">
                <div class="flex justify-between items-center mb-4">
                    <h3 class="text-xl font-semibold">查看离线地图</h3>
                    <button id="closeViewMapModal" class="text-gray-500 hover:text-gray-700 focus:outline-none">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
                    </button>
                </div>
                <div id="offlineMap" class="h-96 w-full rounded-lg"></div>
            </div>
        </div>
    </div>

    <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <script src="https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js"></script>
    <script src="/static/js/main.js"></script>
</body>

</html>